package edu.pku.sei.sct.primitives;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;

import edu.pku.sei.AMT.ObjectVariable;
import edu.pku.sei.ocl.evaluation.Context;
import edu.pku.sei.ocl.evaluation.Environment;
import edu.pku.sei.sct.evaluation.SCTEnvironment;

public class CreateElement extends AbstractPrimitive {
	public static final String ID = "CreateElement";

	public CreateElement(SCTEnvironment e) {
		super(e);
	}

	/**
	 * argv[0] = variable
	 * argv[1] = context
	 */
	@Override
	public Object execute(Object... argv) {
		ObjectVariable v = (ObjectVariable) argv[0];
		Context context = (Context) argv[1];
		System.out.println("Create Element "+v.getName());
		
		EClass type = this.getSctEnvironment().getType(v.getScope(), v.getType());
		EObject o = EcoreUtil.create(type);
		this.sctEnvironment.manager.addObject(v.getScope(), o);
		context.putValue(v.getName(), o);
		Environment.log.create(o);
		return true;
	}

	@Override
	public String getID() {
		return ID;
	}

}
